﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.LookupPicker (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.Fields
{
    /// <summary>
    /// The field control containing the <see cref="LookupPicker"/>.
    /// </summary>
    public class LookupPickerFieldControl : BaseFieldControl
    {
        /// <summary>
        /// The lookuppicker itself.
        /// </summary>
        protected LookupPicker LookupPicker;

        /// <summary>
        /// Creates the <see cref="LookupPicker"/> if the current form is either a new-form or a display-form.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (ControlMode == SPControlMode.New || ControlMode == SPControlMode.Edit)
            {
                var field = (LookupPickerField)Field;
                LookupPicker = new LookupPicker()
                {
                    PickerData = new LookupPickerData(field.LookupWebId,
                        field.LookupListId,
                        field.LookupField,
                        field.AdditionalSearchFields,
                        field.AdditionalDisplayFields,
                        "ID",
                        field.AllowMultipleValues,
                        field.AllowShowAll,
                        field.ContentTypes,
                        field.SearchScope)
                };
                Controls.Add(LookupPicker);
            }
        }

        /// <summary>
        /// Validates whether a required field contains a value.
        /// </summary>
        public override void Validate()
        {
            if (Field.Required && LookupPicker != null && LookupPicker.ResolvedEntities.Count == 0)
            {
                IsValid = false;
                ErrorMessage = SPResource.GetString("MissingRequiredField", new object[0]);
            }
            base.Validate();
        }

        /// <summary>
        /// Gets or sets the value of the field.
        /// The type must be either a <see cref="SPFieldLookupValue"/> or a <see cref="SPFieldLookupValueCollection"/>.
        /// </summary>
        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                if (LookupPicker != null)
                {
                    ArrayList resolvedEntities = LookupPicker.ResolvedEntities;
                    if (LookupPicker.PickerData.AllowMultiValue)
                    {
                        var resultCollection = new SPFieldLookupValueCollection();
                        foreach (PickerEntity entity in resolvedEntities)
                        {
                            resultCollection.Add(new SPFieldLookupValue(int.Parse(entity.Key), entity.DisplayText));
                        }
                        return resultCollection;
                    }
                    else
                    {
                        if (resolvedEntities.Count > 0)
                        {
                            PickerEntity entity = resolvedEntities[0] as PickerEntity;
                            return new SPFieldLookupValue(int.Parse(entity.Key), entity.DisplayText);
                        }
                    }
                }
                return null;
            }
            set
            {
                this.EnsureChildControls();
                if (LookupPicker != null)
                {
                    LookupPicker.Entities.Clear();
                    if (value is SPFieldLookupValueCollection)
                    {
                        ArrayList list = new ArrayList();
                        SPFieldLookupValueCollection lookupValues = value as SPFieldLookupValueCollection;
                        foreach (SPFieldLookupValue lookupValue in lookupValues)
                        {
                            PickerEntity entity = LookupPicker.GetEntity(lookupValue.LookupId);
                            if (entity != null)
                            {
                                list.Add(entity);
                            }
                        }
                        LookupPicker.UpdateEntities(list);
                    }
                    else
                    {
                        if (value is SPFieldLookupValue)
                        {
                            SPFieldLookupValue lookupValue = value as SPFieldLookupValue;
                            PickerEntity entity = LookupPicker.GetEntity(lookupValue.LookupId);
                            if (entity != null)
                            {
                                LookupPicker.UpdateEntities(new ArrayList() { entity });
                            }
                        }
                    }
                }
            }
        }
    }
}
